require "open-uri"

class City < ActiveRecord::Base
  
  belongs_to :province
  has_many :areas
  
  validates_uniqueness_of :name,
    :weather_com_cn_code
    
  def full_name
    province.name == name ? name : (province.name + name)
  end

  def self.find_by_ip(ip)
    location = NumberLocation.new ip
    first :conditions => [
      "? like CONCAT('%省%',name,'%')",
      location.location
    ] if location.ip?
  end
  
  def city_area
    Area.find_by_city_id_and_weather_com_cn_code(id,weather_com_cn_code)
  end
  
end
